local rouzha = fk.CreateSkill{
    name = "jinld__rouzha",
}

Fk:loadTranslationTable{
    ["jinld__rouzha"] = "肉柞",
    [":jinld__rouzha"] = "准备阶段，你可以令所有其他角色依次选择是否交给你任意张牌，然后你须弃置一张牌并跳过本回合的出牌阶段和弃牌阶段，"..
    "若有角色以此法交给你的牌为全场最多，你可以于结束阶段弃置一张牌，然后令其于本回合结束后执行一个额外回合并获得“允玺”直到此额外回合结束。",

    ["#jinld__rouzha_give"] = "肉柞：选择任意张牌，交给 %src",
    ["#jinld__rouzha_choose"] = "肉柞：选择一名其他角色和一张牌，弃置此牌并令该角色于本回合结束后获得一个额外回合",
}

rouzha:addEffect(fk.EventPhaseStart,{
    anim_type = "special",
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(rouzha.name) and player.phase == Player.Start
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room:getOtherPlayers(player), function (p) return not p:isNude() end)
        room:sortByAction(targets)
        local num = 0
        for _, p in ipairs(targets) do
            local cards = room:askToCards(p,{
                min_num = 1,
                max_num = #p:getCardIds("he"),
                pattern = ".",
                skill_name = rouzha.name,
                prompt = "#jinld__rouzha_give:"..player.id,
                cancelable = true,
            })
            if #cards > 0 then
                room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, rouzha.name, nil, false, p)
                room:setPlayerMark(p, rouzha.name.."-turn", #cards)
                if num == 0 then
                    num = #cards
                    room:setBanner("jinld__rouzha_max-turn", #cards)
                else
                    if #cards >= num then
                        num = #cards
                        room:setBanner("jinld__rouzha_max-turn", #cards)
                    end
                end
            end
        end
        if player:isNude() then return end
        room:askToDiscard(player,{
            min_num = 1,
            max_num = 1,
            pattern = ".",
            skill_name = rouzha.name,
            cancelable = false,
        })
        player:skip(Player.Play)
        player:skip(Player.Discard)
    end,
})

rouzha:addEffect(fk.EventPhaseStart,{
    anim_type = "special",
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(rouzha.name) and player.phase == Player.Finish
    end,
    on_cost = function (self, event, target, player, data)
        local n = player.room:getBanner("jinld__rouzha_max-turn") or 0
        if n == 0 then return end
        local targets = table.filter(player.room:getOtherPlayers(player), function (p) return p:getMark(rouzha.name.."-turn") >= n end)
        if #targets == 0 then return end
        local to, cards = player.room:askToChooseCardsAndPlayers(player,{
            min_card_num = 1,
            max_card_num = 1,
            min_num = 1,
            max_num = 1,
            targets = targets,
            pattern = ".",
            skill_name = rouzha.name,
            prompt = "#jinld__rouzha_choose",
            cancelable = true,
            will_throw = true,
        })
        if #to > 0 and #cards > 0 then
            event:setCostData(self, { tos = to, cards = cards})
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        local card = event:getCostData(self).cards
        room:throwCard(card, rouzha.name, player, player)
        room:handleAddLoseSkills(to, "jinld__yunxi_other")
        to:gainAnExtraTurn(true, rouzha.name)
    end,
})

rouzha:addEffect(fk.TurnEnd,{
    late_refresh = true,
    can_refresh = function (self, event, target, player, data)
        return target == player and player:hasSkill("jinld__yunxi_other")
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:handleAddLoseSkills(player, "-jinld__yunxi_other")
    end,
})

return rouzha